<?php

if (!defined('IN_XLP')) {
    exit('Access Denied!');
}
/*
		T('db/mogo');
		$mongo = new mogo("chat");
		$insert['status']=0;
		$insert['num']=0;
		$insert['uid']=288;
		$insert['fuid']=2;
		$insert['dateline']=TIME;
		z($mongo->update("relation",array("uid"=>288),array("status"=>1,"dateline"=>TIME)));
		z($mongo->insert("relation",$insert));
		z($mongo->delete("relation",$delete));
        z($mongo->count("relation",array("status"=>1)));


		$mongo = new mongo("chat");
		$db = $mongo->chat;//选择数据库
        $collection = $db->relation;//选择foobar集合
        $obj = $collection->count();
        z($mongo->find());
*/
class mogo {

    private $db;

    public function __construct($host="",$dbname) {
        
		if($host){
            // $mongo = new Mongo("mongodb://".$host);
		    $mongo = new MongoClient("mongodb://".$host);
		}else{
            // $mongo = new Mongo();
		    $mongo = new MongoClient();
		}
		$this->db = $mongo->$dbname;//选择数据库
    }

    /**
     * 查询
     * @param string $appid
     */
    public function find($table,$where=array()) {
		$collection = $this->db->$table;//选择foobar集合
		$cursor = $collection->find($where);
		$list=array();
		foreach ($cursor as $id => $value) {
			$list[$id]=$value;
		}
        return $list;
    }
    /**
     * 统计
     * @param string $appid
     */
    public function count($table,$where=array()) {
		$collection = $this->db->$table;//选择foobar集合
        $count = $collection->count($where);
        return $count;
    }
    /**
     * 插入数据
     * @param string $appid
     */
    public function insert($table,$data) {
		$collection = $this->db->$table;//选择foobar集合
        $count = $collection->insert($data);
        return $count;
    }
    /**
     * 插入数据
     * @param string $appid
     */
    public function update($table,$condition,$data) {
		$collection = $this->db->$table;//选择foobar集合
        $count = $collection->update($condition,array('$set'=>$data));
        return $count;
    }
    /**
     * 删除数据
     * @param string $appid
     */
    public function delete($table,$condition) {
		$collection = $this->db->$table;//选择foobar集合
        $count = $collection->remove($condition);
        return $count;
    }
}